package git.soulbgm.pojo;

import cn.afterturn.easypoi.excel.annotation.Excel;
import git.soulbgm.pojo.dto.*;
import git.soulbgm.utils.NumberUtil;

import java.util.Date;
import java.util.List;

/**
 * 硬件使用情况信息表
 *
 * @author SoulBGM
 * @date 2020-11-18 10:03:05
 */
public class NodeUsedInfo {

    /**
     * ID
     */
    private Long id;

    /**
     * 节点/主机编号
     */
    @Excel(name = "主机编号", type = 10)
    private Integer nodeCode;

    /**
     * 主机名
     */
    @Excel(name = "主机名")
    private String hostname;

    /**
     * IP地址
     */
    @Excel(name = "IP地址")
    private String ip;

    /**
     * 更新时间
     */
    @Excel(name = "更新时间", format = "yyyy-MM-dd HH:mm:ss")
    private Date detectionTime;

    /**
     * 总磁盘大小 单位字节
     */
    @Excel(name = "总磁盘大小(字节)", type = 10)
    private Long diskTotal;

    /**
     * 已使用磁盘大小 单位字节
     */
    @Excel(name = "已使用磁盘大小(字节)", type = 10)
    private Long diskUsed;

    /**
     * 磁盘使用率
     */
    @Excel(name = "磁盘使用率", numFormat = "#.##%")
    private Double diskUsage;

    /**
     * CPU核心数
     */
    @Excel(name = "CPU核心数", type = 10)
    private Integer cpuNum;

    /**
     * CPU使用率
     */
    @Excel(name = "CPU使用率", numFormat = "#.##%")
    private Double cpuUsed;

    /**
     * 内存总量 单位字节
     */
    @Excel(name = "内存总量", numFormat = "0.00")
    private Double memoryTotal;

    /**
     * 已用内存 单位字节
     */
    @Excel(name = "已用内存", numFormat = "0.00")
    private Double memoryUsed;

    /**
     * 网络接口名称
     */
    @Excel(name = "网络接口名称")
    private String networkName;

    /**
     * 每秒接收数据包数
     */
    @Excel(name = "数据包接收量", type = 10)
    private Long packetsReceiveAmount;

    /**
     * 每秒发送数据包数
     */
    @Excel(name = "数据包发送量", type = 10)
    private Long packetsSendAmount;

    /**
     * 每秒发送字节数
     * 上行bps 单位字节
     */
    @Excel(name = "上行bps", type = 10)
    private Long uploadBps;

    /**
     * 每秒接收字节数
     * 下行bps 单位字节
     */
    @Excel(name = "下行bps", type = 10)
    private Long downloadBps;

    /**
     * 区域编号 0代表不区分区域 其他为自定义
     */
    @Excel(name = "区域编号")
    private Integer regionCode;

    /**
     * 转换
     *
     * @param server 服务器
     * @param info   信息
     */
    public static void convert(Server server, NodeUsedInfo info) {
        Cpu cpu = server.getCpu();
        Mem mem = server.getMem();
        Network network = server.getNetwork();
        List<SysFile> sysFiles = server.getSysFiles();
        long diskTotal = 0;
        long diskUsed = 0;
        for (SysFile s : sysFiles) {
            diskTotal = diskTotal + s.getTotal();
            diskUsed = diskUsed + s.getUsed();
        }
        info.setDiskTotal(diskTotal);
        info.setDiskUsed(diskUsed);
        info.setDiskUsage(NumberUtil.div((double) diskUsed, (double) diskTotal, 10));
        info.setCpuNum(cpu.getLogicalCpuCount());
        info.setCpuUsed(cpu.getTotalUsed());
        info.setMemoryTotal(mem.getTotal());
        info.setMemoryUsed(mem.getUsed());
        info.setNetworkName(network.getName());
        info.setPacketsReceiveAmount(network.getRecvPackets());
        info.setPacketsSendAmount(network.getSendPackets());
        info.setUploadBps(network.getSendBytes());
        info.setDownloadBps(network.getRecvBytes());
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Integer getNodeCode() {
        return nodeCode;
    }

    public void setNodeCode(Integer nodeCode) {
        this.nodeCode = nodeCode;
    }

    public String getHostname() {
        return hostname;
    }

    public void setHostname(String hostname) {
        this.hostname = hostname;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public Date getDetectionTime() {
        return detectionTime;
    }

    public void setDetectionTime(Date detectionTime) {
        this.detectionTime = detectionTime;
    }

    public Long getDiskTotal() {
        return diskTotal;
    }

    public void setDiskTotal(Long diskTotal) {
        this.diskTotal = diskTotal;
    }

    public Long getDiskUsed() {
        return diskUsed;
    }

    public void setDiskUsed(Long diskUsed) {
        this.diskUsed = diskUsed;
    }

    public Double getDiskUsage() {
        return diskUsage;
    }

    public void setDiskUsage(Double diskUsage) {
        this.diskUsage = diskUsage;
    }

    public Integer getCpuNum() {
        return cpuNum;
    }

    public void setCpuNum(Integer cpuNum) {
        this.cpuNum = cpuNum;
    }

    public Double getCpuUsed() {
        return cpuUsed;
    }

    public void setCpuUsed(Double cpuUsed) {
        this.cpuUsed = cpuUsed;
    }

    public Double getMemoryTotal() {
        return memoryTotal;
    }

    public void setMemoryTotal(Double memoryTotal) {
        this.memoryTotal = memoryTotal;
    }

    public Double getMemoryUsed() {
        return memoryUsed;
    }

    public void setMemoryUsed(Double memoryUsed) {
        this.memoryUsed = memoryUsed;
    }

    public String getNetworkName() {
        return networkName;
    }

    public void setNetworkName(String networkName) {
        this.networkName = networkName;
    }

    public Long getPacketsReceiveAmount() {
        return packetsReceiveAmount;
    }

    public void setPacketsReceiveAmount(Long packetsReceiveAmount) {
        this.packetsReceiveAmount = packetsReceiveAmount;
    }

    public Long getPacketsSendAmount() {
        return packetsSendAmount;
    }

    public void setPacketsSendAmount(Long packetsSendAmount) {
        this.packetsSendAmount = packetsSendAmount;
    }

    public Long getUploadBps() {
        return uploadBps;
    }

    public void setUploadBps(Long uploadBps) {
        this.uploadBps = uploadBps;
    }

    public Long getDownloadBps() {
        return downloadBps;
    }

    public void setDownloadBps(Long downloadBps) {
        this.downloadBps = downloadBps;
    }

    public Integer getRegionCode() {
        return regionCode;
    }

    public void setRegionCode(Integer regionCode) {
        this.regionCode = regionCode;
    }
}